package linkedlist;

import exception.OutOfIndexException;

public class MyLinkedList {

    static class ListNode {

        private int val;
        private ListNode next;
        private ListNode prev;

        public ListNode(int val){
            this.val = val;
        }
    }

    private ListNode head;
    private ListNode last;

    //计算链表的长度
    public int size(){
        int count = 0;
        ListNode cur = head;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    //打印链表
    public void display(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val+"  ");
            cur = cur.next;
        }
        System.out.println();
    }

    //判断链表中是否存在关键字key
    public boolean contains(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
            last = node;
        }else{
            node.next = head;
            head.prev = node;
            head = node;
        }
    }

    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
            last = node;
        }else{
            last.next = node;
            node.prev = last;
            last = node;
        }
    }

    //在任意位置插入
    public void addIndex(int index, int data){
        checkIndex(index);
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }

        ListNode node = new ListNode(data);
        ListNode cur = searchIndex(index);
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }

    //查找下标对应的节点
    private ListNode searchIndex(int index){
        ListNode cur = head;
        while(index > 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }


    //检查位置是否合法
    private void checkIndex(int index){
        if(index < 0 || index > size()){
            throw new OutOfIndexException("index 不合法！"+index);
        }
    }

    //删除链表中第一次出现的值为key的节点
    public void remove(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                if(cur == head){
                    head = head.next;
                    //考虑只有一个节点的情况
                    if(head != null){
                        head.prev = null;
                    }else {
                        last = null;
                    }
                }else{
                    if(cur.next != null){
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }else{
                        last = last.prev;
                        last.next = null;
                    }

                }
                return;
            }else{
                cur = cur.next;
            }
        }
    }

    //删除链表中所有值为key的节点
    public void removeAllKey(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                if(cur == head){
                    head = head.next;
                    if(head != null){
                        head.prev = null;
                    }else {
                        last = null;
                    }
                }else{
                    if(cur.next != null){
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }else{
                        last = last.prev;
                        last.next = null;
                    }
                }
                cur = cur.next;
            }else{
                cur = cur.next;
            }
        }
    }

    //清空链表
    public void clear(){
        ListNode cur = head;
        while(cur != null){
            ListNode curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        head = last = null;
    }


}
